package cn.e4j.modular.crm.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwbzT;
import cn.e4j.modular.crm.service.GwbzTService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/crm/gwai306")
public class Gwai306Controller extends BaseController {

    @Autowired
    GwbzTService gwbzTService;

    /**
     * 跳转至客户黑名名称主界面
     *
     * @return
     */
    @RequiresPermissions("crm:gwai306:init")
    @RequestMapping(value = {"", "/init"})
    public String index() {
        return "/pc/crm/gwai306.html";
    }

    @RequiresPermissions("crm:gwai306:init")
    @RequestMapping("/getHeadList")
    @ResponseBody
    public ResponseData getHeadList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        List<Map<String, Object>> list = gwbzTService.getGwbzList(page, inDto);
        return new PageReData(page, list);
    }

    @RequiresPermissions("crm:gwai306:add")
    @RequestMapping("/add")
    @ResponseBody
    public ResponseData add(GwbzT gwbzT) {
        if (gwbzTService.existedTheParams(gwbzT)) {
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        }
        gwbzTService.add(gwbzT);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwai306:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public ResponseData edit(GwbzT gwbzT) {
        gwbzTService.edit(gwbzT);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwai306:del")
    @RequestMapping("/del")
    @ResponseBody
    public ResponseData delRow(GwbzT gwbzT) {
        gwbzTService.delete(gwbzT);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwai306:del")
    @RequestMapping("/delBatch")
    @ResponseBody
    public ResponseData deleteBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbzT> gwbyTList = inDto.getReqList(GwbzT.class);
        gwbzTService.deleteBatch(gwbyTList);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwai306:init")
    @RequestMapping("/updateStatus")
    @ResponseBody
    public ResponseData updateStatus(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbzT> gwbzTList = inDto.getReqList(GwbzT.class);
        gwbzTService.updateStatus(gwbzTList);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/importsData", method = RequestMethod.POST)
    @RequiresPermissions("crm:gwai306:init")
    @ResponseBody
    public ResponseData importsUser(MultipartFile file, ModelMap map) {
        ShiroUser user = ShiroKit.getUser();
        ImportParams params = new ImportParams();
        params.setTitleRows(0);//表格标题行数,默认0
        params.setHeadRows(1);//表头行数,默认1
        params.setStartRows(0);//字段真正值和列标题之间的距离 默认0
        int count = 0;
        try {
            List<GwbzT> list = ExcelImportUtil.importExcel(file.getInputStream(), GwbzT.class, params);
            for (GwbzT gwbzT : list) {
                gwbzT.setGWBZENT(user.getDBC());
                gwbzT.setGWBZACTI("Y");
                if (gwbzTService.existedTheParams(gwbzT)) {//已存在更新
                    this.gwbzTService.edit(gwbzT);
                } else {//
                    this.gwbzTService.add(gwbzT);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData(BizExceptionEnum.SERVER_ERROR.getCode(),
                    BizExceptionEnum.SERVER_ERROR.getMessage());
        }
        return SUCCESS_TIP;
    }
}
